import * as api from '../../api/api.js';
import db from '../../utils/db.js';
import { ASSETS } from '../../config.js';
const app = getApp();
const os = app.os;

Page({
  data: {
    assets: ASSETS,
    topHeight: 0,
    appType: app.globalData.appType,
    isCanBuy: app.isCanBuy(),
    isAutoLogin: true,
    userInfo: app.globalData.userInfo,
    isLogin: false,
    imId: getApp().globalData.imId,
    showRechargeDialog: false,
    recommend: true,
    onlyShowVip: false,
    mpVersion: false
  },
  onLoad: function (options) {
    os.setNavigationBarTitle({
      title: app.globalData.appName
    });
    const envInfo = app.globalData.envInfo;
    this.setData({
      mpVersion: envInfo?.microapp?.mpVersion || false
    });
  },

  onShow: function () {
    this.setData({
      appType: app.isCanBuy() ? app.globalData.appType:1,
      userInfo: app.globalData.userInfo,
      isCanBuy: app.isCanBuy(),
      imId: getApp().globalData.imId,
      isLogin: app.checkLogin()
    })
    this.refreshUserInfo();
    if (app.globalData.isTestEnv) {
      this.setData({
        mpVersion: 'test'
      });
    }
    app.delayAdDialog();
  },

  /**
   * 页面隐藏
   */
  onHide() {
    app.delayAdDialog(false);
  },

  onReady: function () {
    let res = os.getMenuButtonBoundingClientRect();
    this.setData({
      topHeight: (res.bottom || 90) + 10
    });
    app.sendAppLogs('app', 3); // 日志
  },

  refreshUserInfo(){
    app.refreshUserInfo((data)=>{
      this.setData({userInfo: data});
    });
  },

  switchChange: function(e){

  },

  openRechargeDialog(e) {
    os.hideTabBar({});
    this.setData({
      onlyShowVip: e.currentTarget.dataset.vip,
      showRechargeDialog: true,
    });
  },
  
  onRechargeCancel(e) {
    this.hideRechargeDialog();
  },
  
  hideRechargeDialog() {
    this.setData({
      showRechargeDialog: false
    });
    setTimeout(function(){
      os.showTabBar({});
    }, 200);
  },

  onRechargeConfirm(){
    this.refreshUserInfo();
    this.hideRechargeDialog();
  },

  onPlatformImError(e){
    app.Tips({
      title: "即将开通客服，请耐心等待"
    });
  },

  onPlatformImSucess: function(e){
    app.sendAppLogs('app', 4, {
      method_mark: 'im',
      method_mark_1: '官方客服'
    });
  },

  onRecord: function(){
    os.navigateTo({
      url: '/pages/mine/record/record'
    });
  },

  onUserLogin: function(){
    const that = this;
    if (this.data.isLogin) {
      return;
    }

    // if (this.data.isAutoLogin) {
    //   os.getUserProfile({
    //     success(uRes) {
    //       os.login({
    //         force: true,
    //         success(res) {
    //           os.showLoading({
    //             title: '请稍候...',
    //             mask: true
    //           });
    //           let data = app.getLogsData('app', 2);
    //           data.code = res.code;
    //           data.avatar_url = uRes.userInfo.avatarUrl;
    //           data.nickname = uRes.userInfo.nickName;
    //           data.encryptedData = uRes.encryptedData;
    //           data.signature = uRes.signature;
    //           api.userLogin(data).then(ret => {
    //             os.hideLoading({});
    //             if (ret.code == 200) {
    //               // console.log(ret)
    //               app.checkSession();
    //               app.updateUserInfo(ret.data);
    //               that.setData({
    //                 userInfo: ret.data,
    //                 isLogin: true
    //               });
    //               app.Tips({
    //                 title: '登录成功',
    //                 icon: "success"
    //               },{tab: 3, url: 1});
    //             }else{
    //               app.Tips({
    //                 title: ret.msg,
    //                 icon: "fail"
    //               });
    //             }
    //           }).catch(err => {
    //             os.hideLoading({});
    //           });
    //         },
    //         fail(res) {
    //           app.Tips({
    //             title: '登录失败',
    //             icon: 'fail'
    //           });
    //         },
    //       });
    //     },
    //     fail(res) {
    //       app.Tips({
    //         title: '登录失败',
    //         icon: 'fail'
    //       });
    //     },
    //   });

    //   return;
    // }
    
    // os.navigateTo({
    //   url: '/pages/login/index'
    // });
  },

  onVersion: function() {
    if (!this.clickNum) {
      this.clickNum = 0
    }
    this.clickNum += 1;
    if (this.clickNum >= 10) {
      if (db.getEnvConf() === 'test') {
        db.saveEnvConf('pro');
        app.setGlobalData('isTestEnv', false);
        this.setData({
          mpVersion: 'pro'
        });
      } else {
        db.saveEnvConf('test');
        app.setGlobalData('isTestEnv', true);
        this.setData({
          mpVersion: 'test'
        });
      }
      this.clickNum = 0;
    }
  },

  onSetupPage: function(){
    os.navigateTo({
      url: '/pages/setup/setup'
    });
  }
})